package com.meilai.project.controller.web.administration.goods;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.administration.goods.GoodsManageDTO;
import com.meilai.project.service.administration.goods.GoodsManageService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "物品管理", tags = {"物品管理"})
@RestController
@RequestMapping("api/web/goods_manage")
public class GoodsManageController {

    @Autowired
    private GoodsManageService service;


    @GetMapping("list")
    @ApiOperation(value = "物品列表")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    public CommonResult list(@RequestParam(name = "page", defaultValue = "1") Integer page,
                             @RequestParam(name = "page_size", defaultValue = "10") Integer page_size,
                             @ApiParam(name = "supplier_id", value = "供应商ID", required = false)
                             @RequestParam(value = "supplier_id", required = false) Long supplier_id,
                             @ApiParam(name = "name", value = "物品名称", required = false)
                             @RequestParam(value = "name", required = false) String name) {
        GoodsManageDTO searchDTO = new GoodsManageDTO();
        searchDTO.setSupplier_id(supplier_id);
        searchDTO.setName(name);
        return new CommonResult().success(service.selectPageList(page, page_size, searchDTO));
    }

    @GetMapping("no_page_list")
    @ApiOperation(value = "不分页物品列表")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    public CommonResult noPageList(@ApiParam(name = "supplier_id", value = "供应商ID", required = false)
                             @RequestParam(value = "supplier_id", required = false) Long supplier_id,
                             @ApiParam(name = "name", value = "物品名称", required = false)
                             @RequestParam(value = "name", required = false) String name) {
        GoodsManageDTO searchDTO = new GoodsManageDTO();
        searchDTO.setSupplier_id(supplier_id);
        searchDTO.setName(name);
        return new CommonResult().success(service.selectList(searchDTO));
    }


    @ApiOperation(value = "删除", notes = "删除")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @DeleteMapping("delete")
    public CommonResult delete(@RequestParam(value = "id", required = true) Long id) {
        try {
            if (service.delete(id)) {
                return new CommonResult().success("删除成功");
            } else {
                return new CommonResult().failed("删除失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @PostMapping("add")
    @ApiOperation(value = "新增")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    public CommonResult add(@RequestBody GoodsManageDTO dto) {
        try {
            if (service.addOne(dto)) {
                return new CommonResult().success("新增成功");
            }
            return new CommonResult().failed("新增失败");
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @PostMapping("edit")
    @ApiOperation(value = "修改")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    public CommonResult edit(@RequestBody GoodsManageDTO dto) {
        if (service.editOne(dto)) {
            return new CommonResult().success("修改成功");
        }
        return new CommonResult().failed("修改失败");
    }

}
